package crane.bomb;

import java.awt.Color;
import javax.swing.JButton;

/**
 * 
 * @author wangqx
 *
 */
public class BombBean extends BombArray{
    
    /**
     * constructor
     *
     * @param xCur
     * @param yCur
     */
    public BombBean(int xCur, int yCur){
        this._xCur = xCur;
        this._yCur = yCur;        
    }    

    /*
     * value
     */
    private int _value = 0;
    
    /*
     * isBomb
     */
    private boolean _isBomb = false;
    
    /*
     * isOpen
     */
    private boolean _isOpen = false;
    
    /*
     * isBombed
     */
    private boolean _isAsBomb = false;
    
    /*
     * isDoubt
     */
    private boolean _isDoubt = false;
    
    /*
     * xCur
     */
    private int _xCur = 0;
    
    /*
     * yCur
     */
    private int _yCur = 0;
    
    /**
     * button
     */
    private JButton button = null;

    /**
     * @return the _isAsBomb
     */
    protected boolean isAsBomb() {
        return _isAsBomb;
    }
    
    /**
     * @param isAsBomb the _isAsBomb to set
     */
    private void setAsBomb(boolean isAsBomb) {
        _isAsBomb = isAsBomb;
    }

    /**
     * @return the isBomb
     */
    protected boolean isBomb() {
        return this._isBomb;
    }
    
    /**
     * @param isBomb the isBomb to set
     */
    protected void setBomb(boolean isBomb) {
        this._isBomb = isBomb;
    }
    
    /**
     * @return the isOpen
     */
    protected boolean isOpen() {
        return this._isOpen;
    }
    
    /**
     * @param isOpen the isOpen to set
     */
    private void setOpen(boolean isOpen) {        
        this._isOpen = isOpen;
    }
    
    /**
     * @return the isDoubt
     */
    private boolean isDoubt() {
        return this._isDoubt;
    }
    
    /**
     * @param isDoubt
     */
    private void setDoubt(boolean isDoubt) {        
        this._isDoubt = isDoubt;
    }
    
    /**
     * @return the value
     */
    protected int getValue() {
        return this._value;
    }
    
    /**
     * @param value the value to set
     */
    protected void setValue(int value) {
        this._value = value;
    }
    
    /*
     * pointMap
     */
    private BombBean[][] _pointArray = null;    
    
    /**
     * @return the pointArray
     */
    protected BombBean[][] getPointArray() {
        return _pointArray;
    }
    
    /**
     * @param pointArray the pointArray to set
     */
    protected void setPointArray(BombBean[][] pointArray) {
        this._pointArray = pointArray;
    }
    
    /**
     * @return the button
     */
    protected JButton getButton() {
        return button;
    }
    
    /**
     * @param button the button to set
     */
    protected void setButton(JButton button) {
        this.button = button;
    }
    
    /**
     * set the Point Open
     * 
     * @param frame
     * @param leftRightFlag true: LeftClick; false:RightClick
     */
    protected void open(BombFrame frame, boolean leftRightFlag){
        
        String key = (this._xCur * frame.getYLength() + this._yCur) + "";
        if (BombArray.CHECKED_POINT_MAP.containsKey(key)){
            return;
        }        
        
        if (this._xCur < 0 || this._xCur > frame.getXLength() - 1){
            return;
        }
        
        if (this._yCur < 0 || this._yCur > frame.getYLength() - 1){
            return;
        }
        
        if (this.isOpen()){
            return;
        }
        
        if (this._isAsBomb && leftRightFlag){
            return;
        }

        Color color = null;
        String picPath = null;
        int remains = frame.getBombArray().getBombRemain();
        if (leftRightFlag){
            // Left click            
            BombArray.CHECKED_POINT_MAP.put(key, null);
            
            if (this._isBomb){
                this.setOpen(true);
                this.button.setBackground(BombColor.C_BTN_WHICH_BOMBED);
                this.button.setIcon(FileUtil.getImage("/images/bomb.gif"));
                new BombAction().setBombed(frame);
                return;
            } else {
                if (this._value == 0){                    
                    // Open Round Point                
                    openRound(frame, this._xCur, this._yCur);
                    color = BombColor.C_BTN_NOT_BOMBED;
                } else {
                    // set number of bombs round
                    picPath = "/images/pic" + this._value + ".gif";
                    color = BombColor.C_BTN_NOT_BOMBED;
                }
                if (this.isDoubt()){
                    setDoubt(false);
                    this.button.setIcon(null);
                }
                this.setOpen(true);
            }
        } else {
            // Right Click
            if (this._isAsBomb){
                remains++;
                this.setAsBomb(false);
                this.setDoubt(true);
                picPath = "/images/picDoubt.gif";
            } else if (this._isDoubt){
                this.setAsBomb(false);
                this.setDoubt(false);
                this.button.setIcon(null);
            } else {            
                remains--;
                this.setAsBomb(true);
                this.setDoubt(false);                
                picPath = "/images/flag.gif";
            }
        }
        frame.remainText.setText("" + remains);
        frame.getBombArray().setBombRemain(remains);
        if (color != null){
            this.button.setBackground(color);
        }
        if (picPath != null){
            this.button.setIcon(FileUtil.getImage(picPath));
        }
    }
    
    /**
     * Open the round point
     *
     * @param frame
     * @param x
     * @param y
     */
    protected void openRound(BombFrame frame, int x, int y){
        
        BombBean[][] array = this.getPointArray();
        if (array == null){
            return;
        }
        BombBean pb = null;
        for (int i = x - 1 ; i <= x + 1; i++) {
            
            if (i < 0 || i > frame.getXLength() - 1){
                continue;
            }
            
            for (int j = y - 1; j <=  y + 1; j++) {
                if (i == x && j == y){
                    continue;
                }                
                
                if (j < 0 || j > frame.getYLength() - 1){
                    continue;
                }
                
                pb = array[i][j];
                if (pb != null){
                    
                    if (pb.isAsBomb()){
                        continue;
                    }
                    
                    pb.open(frame, true);
                }
            }
        }        
    }    
}
